/**
 * @param {String} time	
 * 格式: '2020-03-03 18:59:00'
 */
import {
	apiPostLogin,
	version,
	versionCompare
} from "@/api/login.js"
import {
	apiGetView
} from "@/api/me.js"

function formatDays(times) {
	// 时间转毫秒数
	let minute = 1000 * 60; //1分钟
	let hour = minute * 60; //1小时
	let day = hour * 24; // 1天
	let week = day * 7;
	let halfmonth = day * 15;
	let month = day * 30;
	let year = month * 12;
	let halfyear = month * 12 / 2;
	let year5 = month * 12 * 5;

	let date = new Date()
	let end = new Date(times.replace(/-/g, '/'))
	//两个时间差的毫秒数
	let dis = date.getTime() - end.getTime();

	if (dis < 0) {
		return '00:00'
	}

	//时间差的毫秒数
	let minD = dis / minute;
	let hourD = dis / hour;
	let dayD = dis / day;
	let weekD = dis / week;
	let halfmonthD = dis / halfmonth;
	let monthD = dis / month;
	let yearD = dis / year;
	let halfyearD = dis / halfyear;
	let year5D = dis / year5;


	if (year5D > 1) {
		return '5年前'; //5年
	} else if (yearD >= 1 && yearD <= 5) {
		return parseInt(yearD) + '年前';
	} else if (halfyearD >= 1 && halfyearD <= 2) {
		return '半年前';
	} else if (monthD >= 1 && monthD <= 6) {
		return parseInt(monthD) + '个月前';
	} else if (halfmonthD >= 1 && halfmonthD <= 2) {
		return '半个月前';
	} else if (weekD >= 1 && weekD <= 3) {
		return parseInt(weekD) + '周前';
	} else if (dayD >= 1 && dayD <= 6) {
		return parseInt(dayD) + '天前';
	} else if (hourD >= 1 && hourD <= 24) {
		return parseInt(hourD) + '小时前';
	} else if (minD >= 1 && minD <= 59) {
		return parseInt(minD) + '分钟前';
	} else if (dis > 0 && dis <= minute) {
		return '刚刚';
	}
}

function formatDate(time, format = 'YY-MM-DD hh:mm:ss') {
	let times = time.getTime();
	let date = new Date(times);
	let year = date.getFullYear(),
		month = date.getMonth() + 1,
		day = date.getDate(),
		hour = date.getHours(),
		min = date.getMinutes(),
		sec = date.getSeconds();
	let preArr = Array.apply(null, Array(10)).map(function(elem, index) {
		return '0' + index;
	});
	let newTime = format.replace(/YY/g, year)
		.replace(/MM/g, preArr[month] || month)
		.replace(/DD/g, preArr[day] || day)
		.replace(/hh/g, preArr[hour] || hour)
		.replace(/mm/g, preArr[min] || min)
		.replace(/ss/g, preArr[sec] || sec);
	return newTime;
}
// 返回对应日期
function dates(item) {
	let year = new Date().getFullYear();
	let month = new Date().getMonth();
	let day = new Date().getDate();
	let startTime = '';
	let endTime = '';
	if (item == '本月') {
		startTime = formatDate(new Date(year, month, 1), 'YY-MM-DD');
		endTime = formatDate(new Date(), 'YY-MM-DD');
	} else if (item == '上月') {
		// 获取月份
		let m = new Date(new Date() - (new Date(year, month, 0).getDate()) * 24 * 3600 * 1000).getMonth();
		startTime = formatDate(new Date(year, m, 1), 'YY-MM-DD');
		endTime = formatDate(new Date(year, month, 0), 'YY-MM-DD');
	} else {
		startTime = formatDate(new Date(new Date().getTime() - secTime(item) * 3600 * 24 * 1000), 'YY-MM-DD');
		endTime = formatDate(new Date(), 'YY-MM-DD');
	}
	return [startTime, endTime]
}

function logins(cb, cb2) {
	if (!uni.getStorageSync("logins")) {
		return
	}
	let openInstallValue =  uni.getStorageSync("openInstallValue")
	let putdata = {}
	uni.setStorageSync("logins", false)
	// #ifdef APP-PLUS
	let device_id = ""
	let brand = ""
	let model = ""
	let userinfo = ''
	if (uni.getSystemInfoSync().platform === "android") {
		device_id = plus.device.imei.split(",")[0] || plus.device.imei
		brand = uni.getSystemInfoSync().brand
		model = uni.getSystemInfoSync().model
	} else if (uni.getSystemInfoSync().platform === "ios") {
		device_id = plus.device.uuid
		brand = plus.device.vendor
		model = plus.device.model
	}
	putdata = {
		device_id,
		brand,
		model
	}
	if (openInstallValue) {
		let referral_code  = JSON.parse(openInstallValue).referral_code
		putdata = {
			device_id,
			brand,
			model,
			referral_code
		}
	}
	apiPostLogin(putdata).then(userinfo => {
		uni.setStorageSync('userinfo', userinfo.data);
		console.log(uni.getStorageSync("userinfo"), "用户")
		console.log(cb, typeof cb == 'function')
		uni.setStorageSync("logins", true)
		if (typeof cb2 == 'function') {
			cb2()
		}
	}).catch(() => {
		if (typeof cb == 'function') {
			cb()
		}
	})
	// 版本
	// version().then(r => {
	// 	console.log('版本哈哈哈', r)
	// 	console.log('洛洛咯', versionCompare(r.data.version, '0.0.0'))
	// })

	// #endif
	// #ifdef H5
	let random = Math.ceil(Math.random() * 100000) + new Date().getTime() + '666'

	let code = uni.getStorageSync('userCode')
	if (!code) {
		uni.setStorageSync('userCode', random);
		code = random
	}

	apiPostLogin({
		device_id: code,
		brand: '525',
		model: "828"
	}).then(userinfo => {
		uni.setStorageSync('userinfo', userinfo.data);
	})
	// #endif
}

function newData(number, title = '修改成功') {
	apiGetView().then(resData => {
		uni.setStorageSync("userinfo", resData.data)
		if (!number) number = 1
		uni.showToast({
			title
		})
		setTimeout(() => {
			uni.navigateBack({
				delta: number
			})
		}, 500)
	});
}


// 没有登录过就刷新
function isLogin() {
	// apiGetView().then(resData => {
	// 	uni.setStorageSync("userinfo",resData.data)}
	// ).catch((e)=>{
	//  console.log(e,888)
	//  uni.navigateTo({
	//    url: '/pages/start/index'
	//  });
	// })
}

// 判断滑动方向
function getAngle(angx, angy) {
	return Math.atan2(angy, angx) * 180 / Math.PI;
};

//根据起点终点返回方向 1向上 2向下 3向左 4向右 0未滑动
function getDirection(startx, starty, endx, endy) {
	var angx = endx - startx;
	var angy = endy - starty;
	var result = 0;

	//如果滑动距离太短
	if (Math.abs(angx) < 2 && Math.abs(angy) < 2) {
		return result;
	}

	var angle = getAngle(angx, angy);
	// if (angle >= -135 && angle <= -45) {
	// 	result = 1;
	// } else if (angle > 45 && angle < 135) {
	// 	result = 2;
	// } else if ((angle >= 135 && angle <= 180) || (angle >= -180 && angle < -135)) {
	// 	result = 3;
	// } else if (angle >= -45 && angle <= 45) {
	// 	result = 4;
	// }
	if (angle >= -135 && angle <= -45) {
		result = 1;
	} else if (angle > 45 && angle < 135) {
		result = 2;
	} else if ((angle >= 150 && angle <= 180) || (angle >= -180 && angle < -150)) {
		result = 3;
	} else if (angle >= -30 && angle <= 30) {
		result = 4;
	}

	return result;
}

export {
	formatDays,
	formatDate,
	dates,
	logins,
	newData,
	isLogin,
	getDirection
}
